package service.impl;

import dao.IOrderDao;
import dao.ITradeDao;
import model.Order;
import model.Trade;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import service.IOrderService;
import util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
public class OrderService implements IOrderService {
    @Autowired
    private IOrderDao iOrderDao;
    @Autowired
    private ITradeDao iTradeDao;

    @Override
    public List<Order> query(Order order) {
        return iOrderDao.query(order);
    }

    @Override
    public int totalCount(Order order) {
        return  iOrderDao.totalCount(order);
    }

    @Override
    public List<Order> listByOrderNo(Order order) {
        return  iOrderDao.listByOrderNo(order);
    }

    @Override
    public ModelAndView listOrderByUId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User m =(User)session.getAttribute("user");
        ModelAndView mv= new ModelAndView();
        List<Order> list = iOrderDao.listOrderByUId(m.getId());
        mv.addObject("user", m);
        mv.addObject("list", list);
        mv.setViewName("shop/user_center_order");
        return  mv;
    }

    @Override
    public String updateOrderStatus(Order order) {
        int count = iOrderDao.updateOrderStatus(order);
        if(count <1){
            return "0";
        }else{
            return  "1";
        }
    }

    @Override
    public void shipment(Map<String, String> map) {
        String orderNos = map.get("orderNo");
        System.out.println("一件发货的订单号是："+orderNos);
        String [] orderNoList = orderNos.split(",");
        for(String orderNo : orderNoList){
            Order order = new Order();
            order.setOrderNo(orderNo);
            if(listByOrderNo(order)!=null){
                iOrderDao.shipment(order.getOrderNo());
            }
        }
    }

    @Override
    public void refundMoney(Map<String, String> map) {
        String orderNos = map.get("orderNo");
        System.out.println("一件退款的订单号是："+orderNos);
        String [] orderNoList = orderNos.split(",");
        for(String orderNo : orderNoList){
            Order order = new Order();
            order.setOrderNo(orderNo);
            if(listByOrderNo(order)!=null){
                Order order1 = query(order).get(0);
                System.out.println("---------------");
                System.out.println(order1);
                iOrderDao.refund(order1);
                iOrderDao.refundMoney(order1);
                Trade trade = new Trade();
                trade.setTradeMoney(order1.getOrderMoney());
                trade.setTradeType(3);
                trade.setCreateTime(DateUtil.currentDate());
                trade.setuId(order1.getuId());
                trade.setOrderNo(order1.getOrderNo());
                iTradeDao.insertTrade(trade);

            }
        }
    }

    @Override
    public ModelAndView listRefund() {
        ModelAndView mv = new ModelAndView();
        Order order = new Order();
        order.setOrderStatus(5);
        List<Order> list = iOrderDao.query(order);
        mv.addObject("list", list);
        mv.setViewName("manager/order_refund");
        return  mv;
    }

    @Override
    public String sign(Order order) {
        int count = iOrderDao.updateOrderStatus(order);
        if(count<1){
            return "0";
        }else{
            return "1";
        }
    }


}
